
$(document).ready(function () {

    animate_login1();
    animate_login2();
    animate_login3();
    animate_login4();
    animate_login5();
    animate_login6();
    animate_login7();

    /*生成验证码*/
    create_code();
    //从后台生成验证码

    queryAppByYydm();//查询应用信息展示名称
    // getVerifyCode();
    getFileLogo();//获取应用图标
});
//从后台获取验证码
function getVerifyCode() {
    $.ajax({
        type: "GET",
        dataType: "json",
        async: false,
        url: "application/getVerifyCode",
        success: function (jsonData) {
            if (jsonData.code == 200) {   //查询成功
                console.log(jsonData);
                var ar1 = '';
                var code = jsonData.verifycode;
                for (var i = 0; i < 4; i++) {
                    ar1 += " "+ code[i];
                }
                $(".c-right").text(ar1);
            }
        }
    });
}

function create_code() {
    getVerifyCode(); //输入错误的时候重新刷新验证码
    $(".c-right").click(function () {
        $("#input-error").css("visibility", "hidden");
        // show_code();
        //新方法点击切换code
        getVerifyCode();
    });
}

/*function create_code() {
  function shuffle() {
    /!* var arr = ['1', 'r', 'Q', '4', 'S', '6', 'w', 'u', 'D', 'I', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
         'q', '2', 's', 't', '8', 'v', '7', 'x', 'y', 'z', 'A', 'B', 'C', '9', 'E', 'F', 'G', 'H', '0', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '3', 'R',
         '5', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];*!/
    var arr = ['r', 'Q', '4', 'S', '6', 'w', 'u', 'D','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k','m', 'n', 'p', 'q', '2', 's', 't', '8', 'v', '7',
      'x', 'y', 'z', 'A', 'B', 'C', '9', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', '3', 'R', '5', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    return arr.sort(function () {
      return (Math.random() - .5);
    });
  };
  shuffle();
  function show_code() {
    var ar1 = '';
    var code = shuffle();
    for (var i = 0; i < 4; i++) {
      ar1 += " "+ code[i];
    }
    ;
    //var ar=ar1.join('');
    $(".c-right").text(ar1);
  };
  show_code();
  $(".c-right").click(function () {
    $("#input-error").css("visibility", "hidden");
    // show_code();
    //新方法点击切换code
    getVerifyCode();
  });
}*/

function  animate_login1() {
    $("#login-left-1").animate({top: '180px'}, 1500,function(){
        $("#login-left-1").animate({top: '200px'}, 1500,animate_login1());
    });
}
function  animate_login2() {
    $("#login-left-2").animate({top: '180px'}, 1500,function(){
        $("#login-left-2").animate({top: '200px'}, 1500,animate_login2());
    });
}

function  animate_login3() {
    $("#login-left-3").animate({top: '60px'}, 1500,function(){
        $("#login-left-3").animate({top: '80px'}, 1500,animate_login3());
    });
}


function  animate_login4() {
    $("#login-left-4").animate({top: '80px'}, 1500,function(){
        $("#login-left-4").animate({top: '100px'}, 1500,animate_login4());
    });
}

function  animate_login5() {
    $("#login-left-5").animate({top: '20px'}, 1000,function(){
        $("#login-left-5").animate({top: '50px'}, 1000,animate_login5());
    });
}

function  animate_login6() {
    $("#login-left-6").animate({top: '70px'}, 1000,function(){
        $("#login-left-6").animate({top: '100px'}, 1000,animate_login6());
    });
}
function  animate_login7() {
    $("#login-left-7").animate({top: '130px'}, 1000,function(){
        $("#login-left-7").animate({top: '160px'}, 1000,animate_login7());
    });
}


var isClickFlag = false;
function login() {
    if(isClickFlag)
        return;

    $(".submit-btn").css("background-color","#707b95");
    isClickFlag = true;

    var username = $("#username").val();
    var password = $("#password").val();
    var code = $("#code").val();

    var tempKey=getTempKey();
    var sm4 = new SM4Util();
    password = sm4.encryptData_CBC(password,tempKey);
    var queryParms = {username: username, password: password,tempKey:tempKey,verifycode:code};


    if (username == "" || password == "" || code == "") {
        $("#input-error").children()[1].innerHTML = "账号或密码或验证码为空,请重新输入！";
        $("#input-error").css("visibility", "visible");
        $(".submit-btn").css("background-color","#3769e4");
        isClickFlag = false;
    } else if ($('#code').val().toLowerCase() != $("#code-produce").text().toLowerCase().replace(/\s/ig,'')) {
        $("#input-error").children()[1].innerHTML = "验证码有误,请重新输入！";
        /*重新生成验证码*/
        create_code();
        $("#input-error").css("visibility", "visible");
        $(".submit-btn").css("background-color","#3769e4");
        isClickFlag = false;
    }
    else {

        $.ajax({
            type: "POST",
            dataType: "json",
            data: queryParms,
            //url: "login?username=" + username + "&password=" + password,
            url: "login",
            success: function (jsonData) {
                if (jsonData.code == 200) {//登录成功
                    sessionStorage.setItem("token", jsonData.token);
                    localStorage.setItem("userName", jsonData.userName);
                    localStorage.setItem("username", jsonData.username);
                    localStorage.setItem("jgbm", jsonData.jgbm);
                    localStorage.setItem("jgmc", jsonData.jgmc);
                    localStorage.setItem("zclsh",jsonData.zclsh);
                    localStorage.setItem("showDate",jsonData.showDate);
                    location.href = jsonData.page;
                    // location.href = 'index.html';
                } else {
                    //$("#input-error").children()[1].innerHTML = "账号或密码错误,请重新输入！";
                    $("#input-error").children()[1].innerHTML = jsonData.msg;
                    $("#input-error").css("visibility", "visible");
                    $(".submit-btn").css("background-color","#3769e4");
                    $("#submit").click("login");
                    //alert(jsonData.msg);
                    isClickFlag = false;
                    /*重新生成验证码*/
                    create_code();
                }
            }
        });


    }
}

$(document).keydown(function (event) {
    if (event.keyCode == 13) {
        $("#submit").click();
    }
});


function  getTempKey() {
    function shuffle() {
         var arr = ['1', 'r', 'Q', '4', 'S', '6', 'w', 'u', 'D', 'I', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
             'q', '2', 's', 't', '8', 'v', '7', 'x', 'y', 'z', 'A', 'B', 'C', '9', 'E', 'F', 'G', 'H', '0', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '3', 'R',
             '5', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        return arr.sort(function () {
            return (Math.random() - .5);
        });
    };
    var verificationCode = "";
    var code = shuffle();
    for (var i = 0; i < 16; i++) {
        verificationCode += code[i];
    }
    return verificationCode;
}

/**
 * 查询应用信息展示名称
 */
function queryAppByYydm() {
    var yydm = "DS";   //应用代码
    if (yydm) {
        $.ajax({
            type: "GET",
            dataType: "json",
            // url: "application/queryApp/" + yydm,
            // url: "application/queryApp/"+ yydm,
            url: "application/queryApp",
            success: function (jsonData) {
                if (jsonData.code == 200) {   //查询成功
                    // $('#appForm').populateForm(.app);
                    var zsmc = jsonData.app.zsmc;
                    if( $.trim(zsmc)!=""){
                        $("#zsmc").html($.trim(zsmc));

                        $("#title").html($.trim(zsmc));
                    }
                } else {
                }
            }
        });
    }
}
/**
 * 查询应用信息展示图片
 */
function getFileLogo() {

  　//  $("#YyLogo").html('<img  src="/plat/application/queryLogo/PLAT"  alt=""  onerror="this.src=\'img/logo.png\'" style="width: 39px;height: 25px;"/>');
    $("#YyLogo").html('<img  src="/plat/application/queryLoginLogo/DQMS"  alt=""  onerror="this.src=\'img/login-logo.png\'" class="logo"   /> ');

}

/**
 * 查看密码
 */
function viewPwd() {
    /*$("#password").attr("type","text");
    $("#use").attr("xlink:href","#icon-chakanmimaclose");*/

    var type=$("#password").attr("type");
    if(type=="password"){
        $("#password").attr("type","text");
        $("#use").attr("xlink:href","#icon-chakanmimaclose");
    }else{
        $("#password").attr("type","password");
        $("#use").attr("xlink:href","#icon-mimachakan1");
    }
}

/**
 * 隐藏密码
 */
function hiddenPwd() {
    $("#password").attr("type","password");
    $("#use").attr("xlink:href","#icon-mimachakan1");
}
